package com.mxchip.livestarmobile.mobile;

import android.app.Application;
import android.content.Context;
import android.content.Intent;

import androidx.multidex.MultiDex;

import com.facebook.stetho.Stetho;
import com.google.code.microlog4android.config.PropertyConfigurator;
import com.mxchip.livestarmobile.mobile.graham.SampleSDK;
import com.mxchip.livestarmobile.mobile.repository.RoomClientRepository;
import com.orhanobut.hawk.Hawk;
import com.tencent.bugly.crashreport.CrashReport;
import com.umeng.analytics.MobclickAgent;
import com.umeng.commonsdk.UMConfigure;
import com.mxchip.livestarmobile.mobile.db.AppDatabase;
import com.mxchip.livestarmobile.mobile.util.Log.FileLog;


public class Appli extends Application {

    private static final String TAG = "Appli";
    private static Context context;
    private static final FileLog logger = FileLog.getLogger(Appli.class);
    public AppDatabase getDatabase() {
        return AppDatabase.getInstance(this);
    }
    // 创建服务用于捕获崩溃异常
    private Thread.UncaughtExceptionHandler restartHandler = (thread, ex) -> {
        restartApp();//发生崩溃异常时,重启应用
    };

    public static Context getContext() {
        return context;
    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }

    @Override
    public void onCreate() {
        super.onCreate();
        init();
        /**
         * 注意: 即使您已经在AndroidManifest.xml中配置过appkey和channel值，也需要在App代码中调
         * 用初始化接口（如需要使用AndroidManifest.xml中配置好的appkey和channel值，
         * UMConfigure.init调用中appkey和channel参数请置为null）。
         */
        UMConfigure.init(this, "5d64a5ed4ca357dd9e0005c5", "Umeng", UMConfigure.DEVICE_TYPE_PHONE, null);
        // 选用AUTO页面采集模式
        MobclickAgent.setPageCollectionMode(MobclickAgent.PageMode.MANUAL);
        /**
         * 设置组件化的Log开关
         * 参数: boolean 默认为false，如需查看LOG设置为true
         */
        UMConfigure.setLogEnabled(true);
        // mSocketClient.addCallbackHandler(meetingSocketCallback);
    }

    @Override
    public void onTerminate() {
        super.onTerminate();
        // mSocketClient.removeCallbackHandler(meetingSocketCallback);
        SampleSDK.getInstance().release();
        RoomClientRepository.INSTANCE.destroy();
    }

    private void init() {
        Hawk.init(getApplicationContext()).build();

        context = this.getApplicationContext();
        PropertyConfigurator.getConfigurator(this).configure();
        Thread.setDefaultUncaughtExceptionHandler(restartHandler); // 程序崩溃时触发线程  以下用来捕获程序崩溃异常
        CrashReport.initCrashReport(getApplicationContext());
        Stetho.initializeWithDefaults(this);
    }

    public void restartApp() {
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_MAIN);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
        android.os.Process.killProcess(android.os.Process.myPid());  //结束进程之前可以把你程序的注销或者退出代码放在这段代码之前
    }
}
